package com.ayoka.parenting.mvp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;

import android.support.v4.app.DialogFragment;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.ayoka.parenting.base.LoadingDialogFragment;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.StatusBarUtils;
import com.jincp.common.utils.ToastUtil;
import com.jincp.common.utils.UIUtils;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MVPBaseActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public abstract class MVPBaseActivity<VDB extends ViewDataBinding, V extends BaseView, P extends BasePresenter> extends AppCompatActivity implements BaseView, View.OnClickListener {

    public static final int REQUEST_CODE_FOR_RESULT = 0x999;

    protected String TAG = getClass().getSimpleName();
    protected VDB mDataBinding;
    protected P mPresenter;

    /**
     * 加载对话框
     */
    public DialogFragment dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentLayout();
        configIntentValue();
        if (getContentLayout() != 0) {
            mDataBinding = DataBindingUtil.setContentView(this, getContentLayout());
        }
        mPresenter = getPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        initViews();
        initData(savedInstanceState);
    }

    protected abstract int getContentLayout();

    protected abstract void initViews();

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract P getPresenter();

    protected void onBeforeSetContentLayout() {
        StatusBarUtils.setStatusBar(this, false, false);
        StatusBarUtils.setStatusTextColor(this, true);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        configIntentValue();
    }

    protected void configIntentValue() {
    }

    protected void setCustomToolBar(Toolbar toolBar, ToolBarOptions options) {
        if (toolBar == null || options == null) return;
        setSupportActionBar(toolBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        configToolbarNavigationIcon(toolBar, options);
        configToolbarTitle(toolBar, options);
    }

    protected void configToolbarNavigationIcon(Toolbar toolBar, ToolBarOptions options) {
        if (options.logoId != 0) {
            toolBar.setLogo(options.logoId);
        }
        if (options.backgroundResource != 0) {
            toolBar.setBackgroundResource(options.backgroundResource);
        }
        if (options.navigateId != 0) {
            toolBar.setNavigationIcon(options.navigateId);
            // 这里要注意的就是需要将setSupportActionBar(toolbar)放在setNavigationOnClickListener()之前设置才行。
            toolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            toolBar.setNavigationIcon(null);
        }
    }

    protected void configToolbarTitle(Toolbar toolBar, ToolBarOptions options) {
        // 这里要注意的就是需要将title标题 setSupportActionBar(toolbar)放在之前设置才行。
        TextView tvTitle = (TextView) toolBar.findViewById(R.id.toolbar_title);
        if (tvTitle != null && !TextUtils.isEmpty(options.titleString)) {
            tvTitle.setText(options.titleString);
            tvTitle.setTextColor(options.titleColor);
            Toolbar.LayoutParams params = (Toolbar.LayoutParams) tvTitle.getLayoutParams();
            params.gravity = options.titleGravity;
        }
    }

    /**
     * 显示加载对话框 默认使用R.string.loading
     *
     * @return 返回一个加载对话框
     */
    public void showWaitDialog() {
        showWaitDialog("正在加载中...");
    }

    /**
     * 显示加载对话框
     *
     * @param message 文字显示信息
     */
    @Override
    public void showWaitDialog(String message) {
        try {
            hideWaitDialog();
            // TODO showWaitDialog
            if (dialog == null) {
                dialog = new LoadingDialogFragment.Builder()
                        .setAnimation(0)
                        .setMessage(message)
                        .build();
            }
            dialog.show(getSupportFragmentManager(), "LoadDialogFragment");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏加载对话框
     */
    @Override
    public void hideWaitDialog() {
        if (dialog != null) {
            try {
                dialog.dismissAllowingStateLoss();
                dialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 显示提示框
     *
     * @param content 文字显示content
     */
    @Override
    public void showToast(String content) {
        // TODO showToast
        ToastUtil.getInstance().show(content);
    }

    @Override
    public void onClick(View view) {
    }

    public void finishDelay(){
        if (isFinishing()) return;
        UIUtils.postTaskDelay(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy()");
        hideWaitDialog();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        super.onDestroy();
    }
}
